package com.zm.zmgz.controller;

import com.zm.zmgz.annotationzm.LoginRequired;
import com.zm.zmgz.pojo.LogBean;
import com.zm.zmgz.pojo.LogSearchCriteria;
import com.zm.zmgz.pojo.Response;
import com.zm.zmgz.service.LogViewService;
import com.zm.zmgz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: cjw
 * @Date: 2020/04/08/0:35
 * @Description:日志查看模块控制层
 */

@RestController
@RequestMapping("/logView/index")
public class LogViewController {

    @Autowired
    LogViewService logViewService;

    @Autowired
    UserService userService;

    /***
    * @Description: 获取总记录数
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-04-08 03:30:26
    */
    @LoginRequired // 需要登录
    @GetMapping("/getNumbers")
    public Response getNumbers(@RequestHeader("X-Token") String identificationNumber, LogSearchCriteria logSearchCriteria){
        if(!Objects.isNull(logSearchCriteria.getTimeLimit()) && !Objects.isNull(logSearchCriteria.getTimeLimit2())) {
            logSearchCriteria.setTimeLimitDatecjw();
            logSearchCriteria.setTimeLimitDate2cjw();
        }
        List<LogBean> logBeanList = logViewService.getNumbers(logSearchCriteria);
        int size = 0;
        if(!Objects.isNull(logBeanList)){
            size = logBeanList.size();
        }
        return new Response(20000,"获取总记录数成功!",size);
    }

    /***
    * @Description: 获取日志对象集合
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-04-08 00:39:02
    */
    @LoginRequired // 需要登录
    @GetMapping("/getLogBeans")
    public Response getLogBeans(@RequestHeader("X-Token") String identificationNumber, LogSearchCriteria logSearchCriteria){
        if(!Objects.isNull(logSearchCriteria.getTimeLimit()) && !Objects.isNull(logSearchCriteria.getTimeLimit2())) {
            logSearchCriteria.setTimeLimitDatecjw();
            logSearchCriteria.setTimeLimitDate2cjw();
        }
        List<LogBean> logBeanList = logViewService.getLogBeans(logSearchCriteria);
        return new Response(20000,"获取日志对象集合成功!",logBeanList);
    }

    /***
    * @Description: 获取用户名集合
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-04-08 01:27:29
    */
    @LoginRequired // 需要登录
    @GetMapping("/getUserNames")
    public Response getUserNames(@RequestHeader("X-Token") String identificationNumber){
        List<String> userNames = userService.getAllUserName();
        List<Map<String,String>> mapList = new ArrayList<>();
        for (String userName : userNames) {
            Map<String,String> map = new HashMap<>();
            map.put("value",userName);
            map.put("label",userName);
            mapList.add(map);
        }
        return Objects.isNull(userNames) ? new Response(20000,"用户名集合获取失败!",null) : new Response(20000,"用户名集合获取成功!",mapList);
    }

}
